package com.dongdongshop.controller;

import com.dongdongshop.freemarkerservice.FreemarkerService;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

@Controller
@RocketMQMessageListener(topic = "ids_topic",consumerGroup = "myconsumer_group")
public class RocketMQController implements RocketMQListener<String>, RocketMQPushConsumerLifecycleListener {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private FreemarkerService freemarkerService;
    @Override
    public void onMessage(String s) {
        String[] split = s.split(",");
    }
    @Override
    public void prepareStart(DefaultMQPushConsumer defaultMQPushConsumer) {
        defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                try {
                    MessageExt messageExt = list.get(0);
                    byte[] body = messageExt.getBody();
                    String str = new String(body);
                    str = str.substring(1,str.length()-1);
                    String[] split = str.split(",");
                    freemarkerService.createPage(split);
                }catch (Exception e){
                    return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                }
                //获取message
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
    }
}